import axios from 'axios';
import store from '../store/index'
function objKeySort(obj) {//排序的函数
    var newkey = Object.keys(obj).sort();
    var newObj = '';
    for (var i = 0; i < newkey.length; i++) {
        // newObj[newkey[i]] = obj[newkey[i]];
        newObj =newObj+`&${[newkey[i]]}=${obj[newkey[i]]}`;
    }
    return newObj.substring(1);
}

export default {
    /**
     * get 请求
     * @param url 接口路由
     * @param auth 是否需要带登录信息
     * @returns {AxiosPromise<any>}
     */
    get(url, auth = false) {
        if (auth) {
            return axios.get(url, {headers: {Authorization: 'Your back-end user authenticates information'}});
        } else {
            return axios.get(url);
        }
    },

    /**
     * post 请求
     *
     * @param url 接口路由
     * @param data 接口参数
     * @param auth 是否需要带登录信息
     * @returns {AxiosPromise<any>}
     */
    post(url, data, auth = true) {
        let sha1 = require("sha1")
	    let sign = ''
        try {
            // data.token = store().state.userInfo.token
            // data.user_id = store().state.userInfo.user_id
        } catch (error) {
            
        }
        data.webversion='v1'//接口版本号
	    data.phone_type=0 //手机系统
	    data.phone_model='an1234sss'//手机型号
	    data.phone_version=10//手机系统版本
	    data.app_version="1.0.0"//APP版本
	    data.phone_deviceid='3524dfd1f214'//手机唯一编码
	    data.platform=data.platform?data.platform:3 //普通3商家4
	    let newData = JSON.parse(JSON.stringify(data));
	    newData.key= 'tJ3pewupK6Cj5keYY2givdjxcAdDypwv' //口令
	    newData.ts=Math.ceil(new Date().getTime()/1000)//当前时间戳
	    sign = objKeySort(newData)
        data.ts = newData.ts
        data.sign = sha1(sign.toLowerCase())
        // console.log('sign---------------------',sign.toLowerCase())
        // console.log('sha1加密---------------------',sha1(sign.toLowerCase()))
        // console.log('当前请求参数---------------------',data)
            if (auth) {
                return axios.post(url, data, {headers: {webversion: 'v1'}});
            } else {
                return axios.post(url, data);
            }
        },

    /**
     * put请求
     * @param url 接口路由
     * @param data 接口参数
     * @param auth 是否需要带登录信息
     * @returns {AxiosPromise<any>}
     */
    put(url, data, auth = false) {
        if (auth) {
            return axios.put(url, data, {headers: {Authorization: 'Your back-end user authenticates information'}});
        } else {
            return axios.put(url, data);
        }
    },

    /**
     * 删除
     * @param url 接口路由
     * @param auth 是否需要带登录信息
     * @returns {AxiosPromise}
     */
    del(url, auth = false) {
        if (auth) {
            return axios.delete(url, {headers: {Authorization: 'Your back-end user authenticates information'}});
        } else {
            return axios.delete(url);
        }
    },

    /**
     * 上传文件
     * @param url 接口路由
     * @param file 接口文件
     * @param auth 是否需要带登录信息
     */
    uploader(url, file, auth = false) {
        let param = new FormData();
        param.append('file', file)
        if (auth) {
            return axios.post(url, param, {headers: {Authorization: 'Your back-end user authenticates information'}})
        } else {
            return axios.post(url, param)
        }
    },
}